'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports System.Net
Imports System.Windows.Forms

Public Class ComCen
    Public ISPReturnData As New ISPReturnData
    Private CookieContainer As New System.Net.CookieContainer

    Public Sub New(ByVal ConfigurationData As ConfigurationData)

        Try
            'set rollover date
            SetRolloverDate(ConfigurationData, ISPReturnData)

            Dim strHTMLData As String

            'get usage
            strHTMLData = ISPGet("https://lounge.comcen.com.au/dump/services.stats.usage.php?username=" & ConfigurationData.Username & "&password=" & ConfigurationData.Password & "&connectionType=" & ConfigurationData.ISPConnectionType & "&serviceNumber=" & ConfigurationData.ISPServiceNo, CookieContainer, System.Net.CredentialCache.DefaultCredentials)
            'strHTMLData = My.Computer.FileSystem.ReadAllText("C:\programming\isp\Comcen-Spin\feed2.txt", System.Text.Encoding.ASCII)
            'strHTMLData = ISPGet("https://my.spin.net.au/dump/services.stats.usage.sampleData.txt", CookieContainer, System.Net.CredentialCache.DefaultCredentials)

            If UA.DebugISP = True Then My.Computer.FileSystem.WriteAllText(GetAppDataPath() & "debug.htm", strHTMLData, False, System.Text.Encoding.ASCII)

            If InStr(strHTMLData, "Invalid Username/password") > 0 Then
                ISPReturnData.ErrorMessage = "Login Failed"
                Exit Sub
            End If

            Dim strLines() As String
            Dim strData() As String
            'Dim dataLimitTotal As Integer
            Dim dataLimitUp As Integer
            Dim dataLimitDown As Integer
            Dim dataLimitOpUp As Integer
            Dim dataLimitOpDown As Integer
            Dim dataLimitP As Integer
            Dim dataLimitOP As Integer
            Dim excessType As String
            Dim excessCost As Single
            Dim CostAvailable As Boolean = False
            Dim Cost As Single
            Dim inboundTotal As Single
            Dim outboundTotal As Single
            Dim inboundPeak As Single
            Dim outboundPeak As Single
            Dim inboundOffPeak As Single
            Dim outboundOffPeak As Single

            'break data into lines
            strLines = strHTMLData.Split(vbLf.ToCharArray)
            'loop through each line
            For Each strLine As String In strLines
                If strLine <> "" Then
                    'separate out values
                    strData = strLine.Trim.Split("=".ToCharArray, 2)

                    'store values
                    If strData(0) = "usagePeriodStart" Then
                        'ISPReturnData.StartMonth = CDate(strData(1))
                    ElseIf strData(0) = "usagePeriodEnd" Then
                        'ISPReturnData.EndMonth = CDate(strData(1))
                    ElseIf strData(0) = "inboundPeak" Then
                        inboundPeak = CSng(strData(1))
                    ElseIf strData(0) = "outboundPeak" Then
                        outboundPeak = CSng(strData(1))
                    ElseIf strData(0) = "inboundOffPeak" Then
                        inboundOffPeak = CSng(strData(1))
                    ElseIf strData(0) = "outboundOffPeak" Then
                        outboundOffPeak = CSng(strData(1))
                    ElseIf strData(0) = "dataLimitTotal" Then
                        'dataLimitTotal = CInt(Val(strData(1)))
                    ElseIf strData(0) = "dataLimitUp" Then
                        dataLimitUp = CInt(Val(strData(1)))
                    ElseIf strData(0) = "dataLimitDown" Then
                        dataLimitDown = CInt(Val(strData(1)))
                    ElseIf strData(0) = "dataLimitOpUp" Then
                        dataLimitOpUp = CInt(Val(strData(1)))
                    ElseIf strData(0) = "dataLimitOpDown" Then
                        dataLimitOpDown = CInt(Val(strData(1)))
                    ElseIf strData(0) = "dataLimitP" Then
                        dataLimitP = CInt(Val(strData(1)))
                    ElseIf strData(0) = "dataLimitOP" Then
                        dataLimitOP = CInt(Val(strData(1)))
                    ElseIf strData(0) = "excessType" Then
                        excessType = strData(1)
                    ElseIf strData(0) = "excessCost" Then
                        excessCost = CSng(Val(strData(1)))
                    ElseIf strData(0) = "offPeakPeriodStart" Then
                        'ConfigurationData.OffpeakStartHour = CInt(Val(strData(1)))
                    ElseIf strData(0) = "offPeakPeriodEnd" Then
                        'ConfigurationData.OffpeakEndHour = CInt(Val(strData(1)))
                    ElseIf strData(0) = "lastUpdated" Then
                        'ISPReturnData.LastUpdated = CDate(strData(1))
                        ISPReturnData.LastUpdated = Date.ParseExact(strData(1), "yyyy-MM-dd HH:mm:ss", Nothing)
                    ElseIf strData(0) = "inboundTotal" Then
                        inboundTotal = CSng(Val(strData(1)))
                    ElseIf strData(0) = "outboundTotal" Then
                        outboundTotal = CSng(Val(strData(1)))
                    End If
                End If
            Next

            'data value -2 = unlimited
            'data value -1 = not applicable
            'standard excessType = charged

            'downloads /uploads
            If ConfigurationData.OffpeakAvailable = True Then
                'offpeak available
                ISPReturnData.PeakDownload = inboundPeak
                ISPReturnData.PeakUpload = outboundPeak
                ISPReturnData.OffpeakDownload = inboundOffPeak
                ISPReturnData.OffpeakUpload = outboundOffPeak
            Else
                'no offpeak available
                ISPReturnData.PeakDownload = inboundTotal
                ISPReturnData.PeakUpload = outboundTotal
            End If

            If excessType = "charged" Then
                'calculate excess cost "cents per meg" 
                Cost = (CSng(Val(ConfigurationData.PeakUsageLimit)) - inboundTotal) * CSng(Val(ConfigurationData.ExcessRate))
            End If

            '##########################################################

            ProcessISPData(ConfigurationData, ISPReturnData)

            'check for error
            If ISPReturnData.ErrorMessage <> "" Then Exit Sub

            '##########################################################

            'create graph data
            If ConfigurationData.OffpeakAvailable = False Then
                'downloads
                ISPReturnData.GraphItemList.Add(New GraphItem("Downloads " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), False))
            Else
                'peak
                ISPReturnData.GraphItemList.Add(New GraphItem("Peak " & Format(System.Math.Floor((ISPReturnData.PeakDownload / Val(ConfigurationData.PeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.PeakDownload & " MB)", ISPReturnData.PeakDownload, CSng(Val(ConfigurationData.PeakUsageLimit)), ISPReturnData.PeakNow))
                'offpeak
                ISPReturnData.GraphItemList.Add(New GraphItem("Offpeak " & Format(System.Math.Floor((ISPReturnData.OffpeakDownload / Val(ConfigurationData.OffpeakUsageLimit)) * 1000) / 10, "#0.0") & "% (" & ISPReturnData.OffpeakDownload & " MB)", ISPReturnData.OffpeakDownload, CSng(Val(ConfigurationData.OffpeakUsageLimit)), ISPReturnData.OffpeakNow))
            End If

            'days elapsed
            ISPReturnData.GraphItemList.Add(New GraphItem("Days Elapsed " & Format((ISPReturnData.DaysUsed / (ISPReturnData.DaysUsed + ISPReturnData.DaysLeft)) * 100, "#0") & "% (" & ISPReturnData.DaysUsed & " Days)", ISPReturnData.DaysUsed, ISPReturnData.DaysUsed + ISPReturnData.DaysLeft, False))

            '##########################################################

            'create stats data
            If ConfigurationData.OffpeakAvailable = False Then
                ISPReturnData.TextItemList.Add(New TextItem("D/L Left :", ISPReturnData.PeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("D/L Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                ISPReturnData.TextItemList.Add(New TextItem("U/L :", ISPReturnData.PeakUpload & " MB"))
            Else
                ISPReturnData.TextItemList.Add(New TextItem("Peak Left :", ISPReturnData.PeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Peak Avg. Left :", ISPReturnData.PeakUsageRemainingPerDayDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Peak U/L :", ISPReturnData.PeakUpload & " MB"))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak D/L Left :", ISPReturnData.OffpeakUsageRemainingDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak Avg. Left :", ISPReturnData.OffpeakUsageRemainingPerDayDescription))
                ISPReturnData.TextItemList.Add(New TextItem("Offpeak U/L :", ISPReturnData.OffpeakUpload & " MB"))
            End If

            If CostAvailable = True Then
                ISPReturnData.TextItemList.Add(New TextItem("Excess Cost :", "$" & Format(Cost, "#0.00")))
            End If

            If ConfigurationData.PeakReached = "Shaped" Or ConfigurationData.OffpeakReached = "Shaped" Then
                If ISPReturnData.ShapedNow = False Then
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "NO"))
                Else
                    ISPReturnData.TextItemList.Add(New TextItem("Shaped :", "YES"))
                End If
            End If

            '##########################################################

            'process alerts
            ProcessAlerts(ConfigurationData, ISPReturnData)

        Catch ex As Exception
            ISPReturnData.ErrorMessage = ex.Message
        End Try
    End Sub

End Class
